Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mapping data to props and replacing props #77

Merged
merged 7 commits into from
Sep 27, 2018
Merged

Support mapping data to props and replacing props #77

merged 7 commits into from
Sep 27, 2018

Conversation

troch
Copy link
Collaborator

@troch troch commented Sep 27, 2018

Can be used as a withProps or mapProps like in recompose:

With props (add props)

import { withEffects, toProps } from 'refract'
import { map } from 'rxjs/operators'

const handler = initialProps => effect => {}

const aperture = () => component =>
    component
        .observe('value')
        .pipe(
            map(val => 2 * val),
            map(doubledVal => toProps({ doubledVal }))
        )

const Component = withEffects(handler)(aperture)(BaseComponent)

Map props (replace props)

import { withEffects, asProps } from 'refract'
import { map } from 'rxjs/operators'

const handler = initialProps => effect => {}

const aperture = () => component =>
    component.observe().pipe(
        map(({ val, ...props }) => ({
            ...props,
            doubledVal: 2 * val
        })),
        map(asProps)
    )

const Component = withEffects(handler)(aperture)(BaseComponent)

Handling state

Props become state, a projection of events. No need for a state container, Refract acts as one.

import { map, scan, startWith } from 'rxjs/operators'

const handler = initialProps => value => {}

const aperture = ({ initialValue }) => component =>
    component
        .event('click')
        .pipe(
            scan(total => total + 1, initialValue),
            startWith(initialValue),
            map(total => toProps({ total }))
        )

const Component = withEffects(handler)(aperture)(BaseComponent)

As connect

import { withEffects, asProps } from 'refract'
import { merge } from 'rxjs'
import { map, mapTo } from 'rxjs/operators'

const handler = initialProps => effect => {}

const aperture = ({ store }) => component =>
    merge(
        store.observe(selectorA).pipe(map(a => ({ a }))),
        store.observe(selectorB).pipe(map(b => ({ b }))),
        store.observe(selectorC).pipe(map(c => ({ c })))
    ).pipe(mapTo(toProps))

const Component = withEffects(handler)(aperture)(BaseComponent)

Copy link
Collaborator

@thisRaptori thisRaptori left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think the current structure also prevents passing functions to setState - reckon that could be an issue? I know the FB React team are going to be pushing for the function version to be considered the default for most cases... 😕

base/react/configureComponent.ts Show resolved Hide resolved
@troch
Copy link
Collaborator Author

troch commented Sep 27, 2018

Think the current structure also prevents passing functions to setState - reckon that could be an issue? I know the FB React team are going to be pushing for the function version to be considered the default for most cases... 😕

I don't think it is an issue. Our use case doesn't need the function version, but if we have to use it, we will.

@thisRaptori
Copy link
Collaborator

thisRaptori commented Sep 27, 2018

Cool, looked through the code more thoroughly and can't see anything wrong, so just tests and docs then :shipit:

@troch
Copy link
Collaborator Author

troch commented Sep 27, 2018

And docs 😬

@thisRaptori
Copy link
Collaborator

And examples! 😱

@troch troch added react Something related to React packages preact Something related to Preact packages inferno Something related to Inferno packages rxjs Something related to RxJS bindings xstream Something related to xstream bindings most Something related to Most bindings callbag Something related to Callbag bindings feature A new feature labels Sep 27, 2018
Copy link
Collaborator

@thisRaptori thisRaptori left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👍

@troch troch merged commit dc16b40 into master Sep 27, 2018
@troch troch deleted the to-props branch September 27, 2018 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
callbag Something related to Callbag bindings feature A new feature inferno Something related to Inferno packages most Something related to Most bindings preact Something related to Preact packages react Something related to React packages rxjs Something related to RxJS bindings xstream Something related to xstream bindings
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants